import request from '@/utils/request';
import { baseurl } from './baseurl';
import { hasIsSw, isCustome, isLianxiSite ,isHangda} from '../utils/utils';
export interface getExamListParams {
  uid: string;
  subject: string;
  type:any;
}

export async function getExamList(params: getExamListParams) {
  /* 获取试题列表 */
  const data = new FormData();
  data.append('uid', params.uid);
  data.append('subject', params.subject);
  const is_sw = JSON.parse(localStorage.getItem('userinfo') || '').is_sw;
  if (hasIsSw()) {
    data.append('is_sw', is_sw);
  }
  data.append('type', params.type||'');
  return request(`${baseurl}index/index/student_test`, {
    method: 'POST',
    data,
  });
}
export interface getExamListScoreParams {
  uid: string;
  id: any;
}

export async function getExamListScore(params: getExamListScoreParams) {
  /* 获取试题列表分数 */
  const data = new FormData();
  data.append('uid', params.uid);
  data.append('id', params.id);
  const is_sw = JSON.parse(localStorage.getItem('userinfo') || '').is_sw;
  if (hasIsSw()) {
    data.append('is_sw', is_sw);
  }
  return request(`${baseurl}index/index/score`, {
    method: 'POST',
    data,
  });
}

export interface submitExamParams {
  uid: string;
  pid: any;
  grade: string;
  topic: any;
  answer: any;
  total: string;
  did_total: string;
  correct_total: string;
  duration: string;
  id: string;
  label: any;
  wrong: any;
}

export async function submitExam(params: submitExamParams) {
  /* 提交模拟考试 */
  const is_sw = JSON.parse(localStorage.getItem('userinfo') || '').is_sw;
  const data = {
    uid: params.uid,
    pid: params.pid,
    grade: params.grade,
    topic: params.topic,
    answer: params.answer,
    total: params.total,
    did_total: params.did_total,
    correct_total: params.correct_total,
    duration: params.duration,
    id: params.id,
    label: params.label,
    wrong: params.wrong,
    is_sw,
  };
  if (!hasIsSw()) {
    delete data.is_sw;
  }
  return request(`${baseurl}index/index/topic_user`, {
    method: 'POST',
    data,
  });
}

export interface checkPaperParams {
  uid: string;
  pid: any;
}

export async function checkPaper(params: checkPaperParams) {
  /* 获取试题列表分数 */
  const data = new FormData();
  data.append('uid', params.uid);
  data.append('pid', params.pid);
  const is_sw = JSON.parse(localStorage.getItem('userinfo') || '').is_sw;
  if (hasIsSw()) {
    data.append('is_sw', is_sw);
  }
  return request(`${baseurl}index/index/topic`, {
    method: 'POST',
    data,
  });
}

export async function checkPaper2(params) {
  /* 获取试题列表分数 */
  const data = new FormData();
  data.append('id', params.id);
  const is_sw = JSON.parse(localStorage.getItem('userinfo') || '').is_sw;
  if (hasIsSw()) {
    data.append('is_sw', is_sw);
  }
  if (isCustome() && (params.subject * 1 === 144 || params.subject * 1 === 145)) {
    return request(`${baseurl}index/index/topic_user_detail_optional`, {
      method: 'POST',
      data,
    });
  }
  return request(`${baseurl}index/index/topic_user_detail`, {
    method: 'POST',
    data,
  });
}

export async function topicQuestionBank(params) {
  /* 出题（新题1、错题2、标记题3、错题优先4） */
  const data = new FormData();
  data.append('uid', params.id);
  data.append('type', params.type);
  data.append('subject', params.subject);
  const is_sw = JSON.parse(localStorage.getItem('userinfo') || '').is_sw;
  if (hasIsSw()) {
    data.append('is_sw', is_sw);
  }
  if (isCustome() && (params.subject * 1 === 144 || params.subject * 1 === 145)) {
    return request(`${baseurl}index/index/topic_question_bank_optional`, {
      method: 'POST',
      data,
    });
  }
  return request(`${baseurl}index/index/topic_question_bank`, {
    method: 'POST',
    data,
  });
}
export async function getDoneExam(params) {
  /* 获取做过的试卷列表 */
  const data = new FormData();
  data.append('id_number', params.id_number);
  data.append('page', params.page);
  data.append('student_type', '');
  data.append('name', '');
  data.append('test_name', '');
  data.append('subject', '');
  const is_sw = JSON.parse(localStorage.getItem('userinfo') || '').is_sw;
  if (hasIsSw()) {
    data.append('is_sw', is_sw);
  }
  return request(`${baseurl}index/index/topic_statis`, {
    method: 'POST',
    data,
  });
}
export async function getDoneExam2(params) {
  /* 获取做过的模拟练习列表 */
  const data = new FormData();
  data.append('id_number', params.id_number);
  data.append('page', params.page);
  data.append('student_type', '');
  data.append('name', '');
  data.append('test_name', '');
  data.append('subject', params.subject);
  const is_sw = JSON.parse(localStorage.getItem('userinfo') || '').is_sw;
  if (hasIsSw()) {
    data.append('is_sw', is_sw);
  }
  return request(`${baseurl}index/index/topic_lianxi`, {
    method: 'POST',
    data,
  });
}

export async function clearWrong(params: any) {
  /* 清除模拟卷错题和标记题 */
  const userinfo = JSON.parse(localStorage.getItem('userinfo') || '');
  const data = new FormData();
  data.append('uid', userinfo.id);
  data.append('type', params.type);
  if (hasIsSw()) {
    data.append('is_sw', userinfo.is_sw);
  }
  return request(`${baseurl}index/index/clear_wrong`, {
    method: 'POST',
    data,
  });
}
